package com.shuoyue.ycgk.ui.account.login;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;

import com.shuoyue.ycgk.Constant;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.appcatch.MemeryCatch;
import com.shuoyue.ycgk.appcatch.SPUtils;
import com.shuoyue.ycgk.base.BaseMvpFragment;
import com.shuoyue.ycgk.entity.UserInfo;
import com.shuoyue.ycgk.ui.account.AccountContract;
import com.shuoyue.ycgk.ui.account.ForgetPwdActivity;
import com.shuoyue.ycgk.ui.account.ResetPwdActivity;
import com.shuoyue.ycgk.ui.common.CstWebActivity;
import com.shuoyue.ycgk.ui.main.MainTabActivity;
import com.shuoyue.ycgk.utils.DevicesUtil;
import com.shuoyue.ycgk.utils.SimpleCodeUtil;
import com.shuoyue.ycgk.utils.XToast;

import java.util.UUID;

import butterknife.BindView;
import butterknife.OnClick;
import pub.devrel.easypermissions.EasyPermissions;

import static android.app.Activity.RESULT_OK;

public class FragmentAccountLogin extends BaseMvpFragment implements AccountContract.View {

    @BindView(R.id.phone_num)
    EditText phoneNum;
    @BindView(R.id.password)
    EditText password;
    @BindView(R.id.checkAgreement)
    CheckBox checkAgreement;

    AccountContract.Presenter accountPresenter;


    @Override
    public void initParams(Bundle bundle) {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_acc_login;
    }

    @Override
    protected void initView() {

        phoneNum.setText(SPUtils.getLoginUserName(mContext));
        password.setText(SPUtils.getLastLoginPwd(mContext));
    }

    @Override
    public void initData() {
        accountPresenter = new AccountContract.Presenter();
        accountPresenter.attachView(this);
    }

    void loginCheck() {
        if (!checkAgreement.isChecked()) {
            toast("请阅读并了解用户协议，如果您同意协议，请勾选同意");
            return;
        }
        String phoneStr = phoneNum.getText().toString();
        String passwordStr = password.getText().toString();
        String deviceName = null, imei = null;
//        if (EasyPermissions.hasPermissions(mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE,
//                Manifest.permission.READ_EXTERNAL_STORAGE)) {
//            deviceName = DevicesUtil.getManuFacturer() + DevicesUtil.getModel();
////            imei = DevicesUtil.getIMEI(mContext);
//            imei = SimpleCodeUtil.getSimpleDeviceCode(mContext);
//            if (TextUtils.isEmpty(imei)) {
//                UUID uuid = UUID.randomUUID();
//                imei = uuid.toString();
//                SimpleCodeUtil.writeSimpleDeviceCode(mContext, imei);
//            }
//        } else {
//            EasyPermissions.requestPermissions(this, "需要获取存储权限",
//                    109, Manifest.permission.WRITE_EXTERNAL_STORAGE,
//                    Manifest.permission.READ_EXTERNAL_STORAGE);
//            toast("需要获取存储权限");
//            return;
//        }
        deviceName = DevicesUtil.getManuFacturer() + DevicesUtil.getModel();
        imei = Settings.System.getString(getActivity().getContentResolver(), Settings.Secure.ANDROID_ID);
        if (TextUtils.isEmpty(imei)) {
            imei = deviceName;
        }
        if (phoneStr.length() != 11) {
            XToast.toast(mContext, "请输入正确的手机号码");
            return;
        }
        if (passwordStr.length() < 6) {
            XToast.toast(mContext, "请输入正确的密码");
            return;
        }

        SPUtils.updataLoginUserName(mContext, phoneStr);
        SPUtils.updateUserPwd(mContext, passwordStr);
        accountPresenter.login(phoneStr, passwordStr, imei, deviceName);
    }

    @OnClick({R.id.clear, R.id.submit, R.id.go_regist
            , R.id.app_deal})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.clear:
                phoneNum.setText("");
                break;
            case R.id.submit:
                loginCheck();
                break;
            case R.id.go_regist:
                startActivity(new Intent(mContext, ForgetPwdActivity.class));
                break;

            case R.id.app_deal:
                CstWebActivity.start(mContext,
                        Constant.HOST_INTERFACE
                                + "/api/share/agreement", "用户协议");
                break;

        }
    }

    @Override
    public void loginSuc(UserInfo userInfo) {
        ((LoginActivity) getActivity()).loginSuc(userInfo);
//        if (userInfo != null) {
//            MemeryCatch.token = userInfo.getToken();
//            SPUtils.updateUserInfo(mContext, userInfo);
//            if (((LoginActivity) getActivity()).srequestCode == -1) {
//                getActivity().startActivity(new Intent(mContext, MainTabActivity.class));
//                getActivity().finish();
//            } else {
//                getActivity().setResult(RESULT_OK);
//                getActivity().finish();
//            }
//        }
    }

    @Override
    public void registSuc() {

    }

    @Override
    public void registFail(String msg) {

    }

    @Override
    public void resetSuc() {

    }

    @Override
    public void bindPhone(String openId, String accessToken) {
    }

}
